package com.huw.NewBie;

import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.entity.particle.ParticleSystem;
import org.anddev.andengine.entity.particle.emitter.CircleParticleEmitter;
import org.anddev.andengine.entity.particle.initializer.AccelerationInitializer;
import org.anddev.andengine.entity.particle.initializer.AlphaInitializer;
import org.anddev.andengine.entity.particle.initializer.ColorInitializer;
import org.anddev.andengine.entity.particle.initializer.VelocityInitializer;
import org.anddev.andengine.entity.particle.modifier.ExpireModifier;
import org.anddev.andengine.entity.scene.Scene;

import com.badlogic.gdx.math.Vector2;
import com.light.roller.ResourceManager;
import com.light.roller.Roller;

public class ParticleEffect {
	protected CircleParticleEmitter particleEmitter;
	protected ParticleSystem particleSystem ;
	protected Roller m_game;
	protected Scene m_scene;
	
	protected int ParticleSystemRadius = 30;
	protected float expireTIme = 1f;
	boolean isExpired = true;
	
	public float getExpireTime()
	{
		return expireTIme;
	}
	
	
	boolean getExpired()
	{
		return isExpired;
	}
	
	public ParticleEffect(Roller game)
	{
		m_game = game;
	}
	
	public void onLoadResources()
	{
		ResourceManager.onLoadResources("breakStonePiece",m_game);
	}
	
	public void onLoadScene(final Scene scene)
	{
		m_scene = scene;	
		ParticleSystemRadius = 30;
		expireTIme = 2.2f;
		
		particleEmitter = new CircleParticleEmitter(0, 0, ParticleSystemRadius);
		particleSystem = new ParticleSystem(particleEmitter, 1000, 1000, 10,ResourceManager.getTextureRegion("breakStonePiece"));
	
		particleSystem.addParticleInitializer(new ColorInitializer(0f/256f, 256f/256f, 0f/256f,256f/256f,0f/256f,256f/256f));
		particleSystem.addParticleInitializer(new AlphaInitializer(1));
		//particleSystem.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE);
		//particleSystem.addParticleModifier(new ColorModifier(185f/256f, 0, 122f/256f, 0, 87f/256f, 0, 1.5f, 2));
		//particleSystem.addParticleModifier(new AlphaModifier(1, 0, 1.5f, 2));
		particleSystem.addParticleModifier(new NewBieRotationModifier(4,0,2.2f));			
		
		
		final int shootWidth = 100;
		final int shootHeight = 300;
		final int shootG = 500;				

		particleSystem.addParticleInitializer(new VelocityInitializer(-shootWidth,shootWidth,-shootHeight,-shootWidth));
		particleSystem.addParticleInitializer(new AccelerationInitializer(0,shootG));			
		
		particleSystem.setIgnoreUpdate(true);
	}
	
	public void shoot(Vector2 center)
	{
		if(!isExpired)return;
		
		m_scene.attachChild(particleSystem);		
		particleEmitter.setCenter(center.x-ParticleSystemRadius/2,center.y-ParticleSystemRadius/2);		
		particleSystem.setIgnoreUpdate(false);
		isExpired = false;
		
		IUpdateHandler updateHandler = new IUpdateHandler()
		{
			float counter = 0;
			@Override
			public void onUpdate(float pSecondsElapsed) {
				// TODO Auto-generated method stub
				counter += pSecondsElapsed;
				
				if(counter>=getExpireTime())
				{
					particleSystem.setIgnoreUpdate(true);
					m_scene.detachChild(particleSystem);	
					isExpired = true;
					particleSystem.reset();
					particleEmitter.reset();
					m_scene.unregisterUpdateHandler(this);					
				}
			}

			@Override
			public void reset() {
				// TODO Auto-generated method stub
				
			}
			
		};
		
		m_scene.registerUpdateHandler(updateHandler);
	}
	

	
	
}
